package org.liveboardgames.agricola.vo.card
{
	import com.adobe.cairngorm.vo.IValueObject;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ListCollectionView;
	
	import org.liveboardgames.agricola.vo.common.AlternativeCost;
	import org.liveboardgames.common.dictionary.collections.IMap;

	[RemoteClass(alias="org.liveboardgames.agricola.domain.card.ImprovementCard")]
	[Bindable]
	public class ImprovementCard extends Card implements IValueObject
	{
		public var cost:IMap;

		/** Handled by wrappers. This should work since the alternative costs
		 * are not modifiable and should not be used for data binding. */
		private var _alternativeCosts:ListCollectionView;

		public var types:ListCollectionView;

		public var category:String;

		public function addAlternativeCost(cost:IMap):void
		{
			if (alternativeCosts == null)
			{
				alternativeCosts = new ArrayCollection();
			}
			// Wrapper around the cost itself, for communication with the outside
			var alternativeCost:AlternativeCost = new AlternativeCost;
			alternativeCost.cost = cost;

			alternativeCosts.addItem(alternativeCost);
		}

		public function set alternativeCosts(costs:ListCollectionView):void
		{
			// Reinitialize the collection
			_alternativeCosts = new ArrayCollection();

			for each (var object:Object in costs)
			{
				if (object is IMap)
				{
					addAlternativeCost(object as IMap);
				}
				else if (object is AlternativeCost)
				{
					_alternativeCosts.addItem(object);
				}
				else
				{
					throw new Error("Unexpected alternative cost: " + object);
				}
			}
		}

		public function get alternativeCosts():ListCollectionView
		{
			var costs:ListCollectionView = null;

			if (_alternativeCosts != null)
			{
				costs = new ArrayCollection();

				for each (var alternativeCost:AlternativeCost in _alternativeCosts)
				{
					costs.addItem(alternativeCost.cost);
				}
			}

			return costs;
		}

		/** Really ImprovementTypeEnum */
		public function addType(type:String):void
		{
			if (types == null)
			{
				types = new ArrayCollection();
			}
			types.addItem(type);
		}
	}
}